<?php
/**
 * 公共函数库
 * @copyright Copyright&copy; 2015, 广州六象元一网络科技有限公司
 * @author 王海龙 $
 * @version  $Id: funBase.php, v 1.0, 2015-01-01 王海龙 $
 */


/**
 * 获取广告信息
 *
 * @access  private
 * @param    int   $position_id    广告位ID
 * @return  array
 */
function getAds($position_id = 0){
    if(!$position_id) return false;
    $position_id = intval($position_id);
    $time = time();
    $sql = "SELECT * FROM egg_ad WHERE position_id = '$position_id' AND start_time <= $time AND end_time >= $time AND enabled = 1 ORDER BY sort ASC, ad_name ASC ";
    $data = $GLOBALS['db']->getAll($sql);
    if (!empty($data)) {
        foreach ($data as $key => $value) {
            $data[$key]['url'] = getUrl('ad', $value);
            $data[$key]['img_url'] = getUrl('ad_img', $value);
        }
    }
    return $data;
}

/**
 * 获取当前分类下的所有子类
 * @param  int $cid 分类ID
 * @return arr      所有子类ID数组
 */
function getCatAllChildren($cid) {
    if (!empty($cid) && !is_array($cid)) {
        $cid = array($cid);
    }
    $cStr = implode(',', $cid);
    $sql = "SELECT cat_id FROM egg_category WHERE parent_id IN ($cStr)";
    $cArr = $GLOBALS['db']->getCol($sql);
    if (!empty($cArr)) {
        if ($cArr2 = getCatAllChildren($cArr)) {
            return array_merge($cArr, $cArr2);
        }
        return $cArr;
    }
    return false;
}

/**
 * 获取当前分类下的所有父类
 * @param  int $cid 分类ID
 * @return arr      所有子类ID数组
 */
function getCatAllParent($cid) {
    if (!empty($cid) && !is_array($cid)) {
        $cid = array($cid);
    }
    $cStr = implode(',', $cid);
    $sql = "SELECT parent_id FROM egg_category WHERE cat_id IN ($cStr)";
    $cArr = $GLOBALS['db']->getCol($sql);
    if (!empty($cArr)) {
        if ($cArr2 = getCatAllParent($cArr)) {
            return array_unique(array_filter(array_merge($cArr, $cArr2)));
        }
        return array_unique(array_filter($cArr));
    }
    return false;
}


/**
 * 获取当前分类下的子类
 * @param  int $cid 分类ID
 * @return arr      子类信息数组
 */
function getCatChildren($cid) {
    $sql = "SELECT cat_id, cat_name, cat FROM egg_category WHERE parent_id = ($cid) AND is_show ORDER BY sort_order ASC, cat_id ASC";
    $cArr = $GLOBALS['db']->getAll($sql);
    return $cArr;
}